<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_COST_DM.php' );
require_once( '../system/mdao/Class_M_COST_FLIER.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/aoki_m_plus.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_MATRIX,$login['staff_auth']);

$dosearch = FALSE ;
$sorts       = array(1 => "チラシ", 2 => "DM");
$sortfiless  = array(1 => "FL", 2 => "DM");

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ) {
	$forms = $_GET;
}
if (empty($forms["sort"])) {
	$forms["sort"] = 1;
}

if ( isset( $forms['sort'] ) ){
	$sort  = $forms['sort'];
	 if (  $sort == "1" )  {
		$dao = new Class_M_COST_FLIER;
	 }  elseif (  $sort == "2" )  {
		$dao = new Class_M_COST_DM;
	 } else {
	 	echo "パラメタエラー";
	 	exit;
	 }
}else {
	 echo "パラメタエラー";
	 exit;
}

$wherearr =  array();

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$year4 = $forms["year4"];

if ( isset( $forms['months'] ) &&  ( !empty( $forms['months'] ) ) ){
	 if ($forms['months'] < 4 ) {
	 	$year4 =$year4 + 1;
	 }
	$month = $forms['months']<10 ? "0".$forms['months'] : $forms['months'];
	$month_begin = $year4.$month."01";
	$month_end   = $year4.$month."31";
	$wherearr[] = "( b.flier_date >=  '{$month_begin}' AND b.flier_date <=  '{$month_end}' )";
} else if ( isset($year4 ) &&  ( !empty( $year4) ) ) {
	$month_begin = $year4."0401";
	$month_end   = ($year4+1)."0331";
	$wherearr[] = "( b.flier_date >=  '{$month_begin}' AND b.flier_date <=  '{$month_end}' )";
}

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "{$sortfiless[$sort]}_FORECAST_".date("YmdHis");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.txt");
	$data = $dao->get_txt_data( $wherearr,$forms['year4'] );
	
	$colums = array();
	$col_heads = array(		
		'year4'         => array( '年',              1),
		'month'         => array( '月',              1),
		'sort'          => array( 'データ種別',      1),
		'centercode'    => array( '利益センタ',      1),
		'money'         => array( '見込金額',        1)
	);
	
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	
	//データ整形開始
	foreach ($data as $k => $v) {
		$data[$k]["year4"]           =  $year4 ;
		$data[$k]["sort"]            =  $sortfiless[$sort];
		$data[$k]["centercode"]      =  '201000'.$v["shop_cd"].'0';
	}
	//データ整形完了
	
	$dao->output_csv($data, $colums,'	');
	exit;
}
//---CSVダウンロード　処理完了



//

$smarty->assign('year4',  Class_PWD::get_years());

$months = array();
$months[""] = PLEASE_SELECT_ARR;
for( $a = 1; $a <= 12; $a++ )
{
	$months[$a] = "&nbsp;".$a;
}
$smarty->assign('months', $months );


$smarty->assign('sorts', $sorts );

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
if( isset($page) )  $smarty->assign('page', $page);



$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_csv_index.html');
//データ整形

?>